﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CoinsLibrary
{
    public class OrderBook
    {
        public CoinPrice AB { set; get; }
        public CoinPrice AC { set; get; }
        public CoinPrice BC { set; get; }
        //public double PriceB { set; get; }

        public double AmountB = 0;
        public double AmountC { set; get; }

        public static double Rate = 0.001;
        public static double MaxAmount = 0.2;

        public int Analyse()
        {
            double real_bid = this.AB.bid * this.BC.bid;
            double real_ask = this.AB.ask * this.BC.ask;
            Console.WriteLine(real_bid);
            Console.WriteLine(real_ask);
            double margin_1 = (real_bid - this.AC.ask) / real_bid;
            double margin_2 = (this.AC.bid - real_ask) / this.AC.bid;

            double min_amount = Math.Min(this.AB.bidAmount,this.AB.askAmount);
            if (min_amount < 2)
            {
                return 0;
            }
            
            Console.WriteLine(margin_1);
            Console.WriteLine(margin_2);

            if (margin_1 > Rate)
            {
                double min_coin_amount = Math.Min(this.AC.askAmount,this.BC.bidAmount);
                this.AmountC = min_coin_amount > MaxAmount ? MaxAmount : min_coin_amount;
                this.AmountB = Math.Round(this.AmountC*this.BC.bid, 2);
                return 1;
            }
            if (margin_2 > Rate)
            {
                double min_coin_amount = Math.Min(this.BC.askAmount, this.AC.bidAmount);
                this.AmountC = min_coin_amount > MaxAmount ? MaxAmount : min_coin_amount;
                this.AmountB = Math.Round(this.AmountC * this.BC.bid, 2);
                return 2;
            }
            return 0;
        }
    }

    public struct CoinPrice
    {
        public string symbol;
        public double bid;
        public double ask;
        public double bidAmount;
        public double askAmount;
        public long time;
        override
        public string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }
    /// <summary>
    /// 状态：-1 余额不足 -2 ip未设置 1正常下单 2 完成 0 下单失败
    /// </summary>
    public struct OrderResult
    {
        public string oid;//订单号
        public int status;
        public double price;
        public double amount;
        public long time;
        public string type;
        public string market;
        public string coinname;
        override
        public string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }
}
